<template>
  <el-row v-if="report != null" style="margin-left: 15px">
    <el-descriptions :column="1">
      <el-descriptions-item label="供应商">{{ report.supplier }}</el-descriptions-item>
      <el-descriptions-item label="芯片型号">{{ report.chip_no }}</el-descriptions-item>
      <el-descriptions-item label="锐科编号">{{ report.raycus_no }}</el-descriptions-item>
      <el-descriptions-item label="测试时间">{{ report.test_time }}</el-descriptions-item>
      <el-descriptions-item label="测试内容">{{ report.test_content }}</el-descriptions-item>
      <el-descriptions-item label="器件验证类型">{{ report.module_type }}</el-descriptions-item>
      <el-descriptions-item label="长期可靠性验证报告">
        <el-link type="primary" :href="report.report_file_url" target="_blank">{{ report.report_file_url }}</el-link>
      </el-descriptions-item>
    </el-descriptions>
    <el-table
      v-loading="listLoading"
      :data="params"
      element-loading-text="Loading"
      border
      fit
      size="medium"
      :header-cell-style="headerCellStyle"
      style="margin-top: 20px"
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-descriptions :column="1" direction="vertical" border>
            <el-descriptions-item label="测试数据" style="margin-top: 10px">
              <el-row>
                <el-col :span="8">
                  <el-table
                    :data="scope.row.weeks"
                    :header-cell-style="headerCellStyle"
                    border
                    fit
                    :show-header="false"
                    size="medium"
                  >
                    <el-table-column width="200" align="left">
                      <template slot-scope="weekScope">
                        {{ getWeekDataLable(weekScope.$index, weekScope.row.test_time) }}
                      </template>
                    </el-table-column>
                    <el-table-column align="right">
                      <template slot-scope="weekScope">
                        {{ weekScope.row.test_value }}
                      </template>
                    </el-table-column>
                  </el-table>
                </el-col>
              </el-row>
            </el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column label="场地" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.position }}
        </template>
      </el-table-column>
      <el-table-column label="编号" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.sn }}
        </template>
      </el-table-column>
      <el-table-column label="验证项目" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.project }}
        </template>
      </el-table-column>
      <el-table-column label="器件类型" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.module_type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="老化电流" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.aging_current }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.state }}
        </template>
      </el-table-column>
      <el-table-column label="终测功率" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.final_power }}
        </template>
      </el-table-column>
      <el-table-column label="加速因子" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.af }}
        </template>
      </el-table-column>
      <el-table-column label="累计工作时间" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.total_test_time }}
        </template>
      </el-table-column>
    </el-table>
    <el-row style="margin-top: 50px">
      <el-row>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="工作条件" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="I0">{{ report.rated_current }}</el-descriptions-item>
                <el-descriptions-item label="P0">{{ report.rated_power }}</el-descriptions-item>
                <el-descriptions-item label="Tjct，0">{{ report.rated_junction_temperature }}</el-descriptions-item>
                <el-descriptions-item label="Tjct，0(K)">{{ report.rated_junction_temperature_kelvin }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="加速条件" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="I">{{ report.burn_in_current }}</el-descriptions-item>
                <el-descriptions-item label="P">{{ report.burn_in_power }}</el-descriptions-item>
                <el-descriptions-item label="Tjct">{{ report.burn_in_junction_temperature }}</el-descriptions-item>
                <el-descriptions-item label="Tjct(K)">{{ report.burn_in_junction_temperature_kelvin }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="常数项" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="活化能Ea/ev">{{ report.activation_energy }}</el-descriptions-item>
                <el-descriptions-item label="x因子">{{ report.x_factor }}</el-descriptions-item>
                <el-descriptions-item label="y因子">{{ report.y_factor }}</el-descriptions-item>
                <el-descriptions-item label="玻尔兹曼常数（eV/K）">{{ report.boltzmann_constant }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="加速因子" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="电流加速">{{ report.current_acceleration_factor }}</el-descriptions-item>
                <el-descriptions-item label="功率加速">{{ report.power_acceleration_factor }}</el-descriptions-item>
                <el-descriptions-item label="温度加速">{{ report.temperature_acceleration_factor }}</el-descriptions-item>
                <el-descriptions-item label="AF">{{ report.total_acceleration_factor }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="卡方值" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="置信水平">{{ report.confidence_coefficient }}</el-descriptions-item>
                <el-descriptions-item label="失效数">{{ report.invalid_number }}</el-descriptions-item>
                <el-descriptions-item label="X2">{{ report.chi_square }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="工作寿命" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="累计工作时间">{{ report.total_test_time }}</el-descriptions-item>
                <el-descriptions-item label="失效率λ">{{ report.failure_rate }}</el-descriptions-item>
                <el-descriptions-item label="寿命MTBF">{{ report.mean_time_between_failure }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6">
          <el-descriptions :column="1" direction="vertical">
            <el-descriptions-item label="加速寿命" style="margin-top: 10px">
              <el-descriptions :column="1" border>
                <el-descriptions-item label="加速因子AF">{{ report.acceleration_factor }}</el-descriptions-item>
                <el-descriptions-item label="失效率FR0">{{ report.rated_failure_rate }}</el-descriptions-item>
                <el-descriptions-item label="失效率FR">{{ report.accelerated_failure_rate }}</el-descriptions-item>
              </el-descriptions>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="6" />
      </el-row>
    </el-row>
  </el-row>
</template>

<script>
import { mapGetters } from 'vuex'
import waves from '@/directive/waves'
import { fetchReliabilityReport } from '@/api/reliability_report'

export default {
  name: 'ReliabilityReport',
  directives: { waves },
  // eslint-disable-next-line vue/require-prop-types
  props: ['testTime', 'id'],
  data() {
    return {
      chipId: this.$route.query.chipId,
      reportTestTime: this.testTime,
      reportId: this.id,
      report: null,
      params: [],
      summaryInfos: [{}],
      listLoading: true,
      form: {
        name: 'xxxx',
        desc: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {
    this.fetchData(this.chipId, this.reportId)
  },
  methods: {
    formatWeek(index) {
      if (index === 0) {
        return '第一周'
      } else if (index === 1) {
        return '第二周'
      } else if (index === 2) {
        return '第三周'
      } else if (index === 3) {
        return '第四周'
      } else if (index === 4) {
        return '第五周'
      } else if (index === 5) {
        return '第六周'
      } else if (index === 6) {
        return '第七周'
      } else if (index === 7) {
        return '第八周'
      } else if (index === 8) {
        return '第九周'
      } else if (index === 9) {
        return '第十周'
      } else if (index === 10) {
        return '第十一周'
      } else if (index === 11) {
        return '第十二周'
      } else if (index === 12) {
        return '第十三周'
      } else if (index === 13) {
        return '第十四周'
      } else if (index === 14) {
        return '第十五周'
      } else {
        return '不支持更多了'
      }
    },
    formatDate(testTime) {
      const timestamp = new Date(testTime)
      const y = timestamp.getFullYear()
      const m = timestamp.getMonth() + 1
      const d = timestamp.getDate()
      return y + '-' + m + '-' + d
    },
    getWeekDataLable(index, testTime) {
      // window.console.log(testTime)
      const timestamp = new Date(testTime)
      const y = timestamp.getFullYear()
      const m = timestamp.getMonth() + 1
      const d = timestamp.getDate()
      const dateTime = y + '-' + m + '-' + d
      if (index === 0) {
        return '第一周（' + dateTime + '）'
      } else if (index === 1) {
        return '第二周（' + dateTime + '）'
      } else if (index === 2) {
        return '第三周（' + dateTime + '）'
      } else if (index === 3) {
        return '第四周（' + dateTime + '）'
      } else if (index === 4) {
        return '第五周（' + dateTime + '）'
      } else if (index === 5) {
        return '第六周（' + dateTime + '）'
      } else if (index === 6) {
        return '第七周（' + dateTime + '）'
      } else if (index === 7) {
        return '第八周（' + dateTime + '）'
      } else if (index === 8) {
        return '第九周（' + dateTime + '）'
      } else if (index === 9) {
        return '第十周（' + dateTime + '）'
      } else if (index === 10) {
        return '第十一周（' + dateTime + '）'
      } else if (index === 11) {
        return '第十二周（' + dateTime + '）'
      } else if (index === 12) {
        return '第十三周（' + dateTime + '）'
      } else if (index === 13) {
        return '第十四周（' + dateTime + '）'
      } else if (index === 14) {
        return '第十五周（' + dateTime + '）'
      } else {
        return '不支持更多了（' + dateTime + '）'
      }
    },
    headerCellStyle({ row, rowIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #FAFAFA !important'
      }
    },
    firstCellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        return 'background-color: #FAFAFA !important'
      }
    },
    fetchData(curChipId, curReportId) {
      this.listLoading = true
      fetchReliabilityReport({ chipId: curChipId, reportId: curReportId }).then(response => {
        // window.console.log('this.params', this.params)
        this.report = response.data
        this.params = this.report.params
        this.listLoading = false
      })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // window.console.log(row, column)
      if (columnIndex === 0) {
        if (rowIndex === 0) {
          // 芯片外观
          return {
            rowspan: 4,
            colspan: 1
          }
        } else if (rowIndex === 4) {
          // 共晶质量
          return {
            rowspan: 3,
            colspan: 1
          }
        } else if (rowIndex === 7) {
          // 光电参数
          return {
            rowspan: 13,
            colspan: 1
          }
        } else if (rowIndex === 20) {
          // 光电参数
          return {
            rowspan: 11,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px 0px 80px 0px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.el-table .tableHeaderStyle {
  background-color: #1989fa !important;
  color: #fff;
}

</style>
